﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Instrument;
using System.Configuration;
using System.Drawing.Drawing2D;
using System.Drawing.Text;

namespace UI
{
    public partial class MainForm : Form
    {
        Employee ThisEmployee;
        public static C34970AConfiger _C34970AConfiger;
        Production currentProduction;
        Production CurrentProduction
        {
            get
            {
                return currentProduction;
            }
            set
            {
                currentProduction = value;
                this.Text = "主菜单  当前待测产品：" + currentProduction.Model;
                Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                configuration.AppSettings.Settings["CurrentProductionModel"].Value = currentProduction.Model;
                configuration.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("AppSettings");
            }
        }

        public MainForm(Employee employee)
        {
            InitializeComponent();
            ThisEmployee = employee;

            //初始化C34970A配置
            string model = ConfigurationManager.AppSettings["CurrentProductionModel"];
            CurrentProduction = DataBaseOperation.GetInstance().GetMatchProduction(model);//初始化产品
            _C34970AConfiger = new C34970AConfiger(CurrentProduction);

            if (ThisEmployee.Role == "Ordinary")
            {
                bussinessPanel.Enabled = false;
            }

        }
        [DllImport("dwmapi.dll", PreserveSig = false)]
        static extern void DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS margins);

        [DllImport("dwmapi.dll", PreserveSig = false)]
        static extern bool DwmIsCompositionEnabled();
        /// <summary>
        /// 重写的onLoad方法
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            if (DwmIsCompositionEnabled())
            {
                MARGINS margins = new MARGINS();
                margins.Right = margins.Left = margins.Top = margins.Bottom = this.Width + this.Height;
                DwmExtendFrameIntoClientArea(this.Handle, ref margins);
            }
            base.OnLoad(e);
            //this.Opacity = 0.8;
        }
        /// <summary>
        /// 重绘背景
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);
            if (DwmIsCompositionEnabled())
            {
                e.Graphics.Clear(Color.Black); //将窗体用黑色填充（Dwm 会把黑色视为透明区域）
            }
        }

        private void serialPanel_Paint(object sender, PaintEventArgs e)
        {
            Bitmap bmp = new Bitmap(Properties.Resources.串口配置管理);
            e.Graphics.DrawImage(bmp, 0, 0, serialPanel.Width, serialPanel.Height);
        }

        private void stressSensor_Paint(object sender, PaintEventArgs e)
        {
            Bitmap bmp = new Bitmap(Properties.Resources.应变计);
            e.Graphics.DrawImage(bmp, 0, 0, stressSensor.Width, stressSensor.Height);
        }

        private void bussinessPanel_Paint(object sender, PaintEventArgs e)
        {
            Bitmap bmp = new Bitmap(Properties.Resources.业务管理);
            e.Graphics.DrawImage(bmp, 0, 0, bussinessPanel.Width, bussinessPanel.Height);
        }

        private void recordPanel_Paint(object sender, PaintEventArgs e)
        {
            Bitmap bmp = new Bitmap(Properties.Resources.记录查询);
            e.Graphics.DrawImage(bmp, 0, 0, recordPanel.Width, recordPanel.Height);
        }

        private void startPanel_Paint(object sender, PaintEventArgs e)
        {
            Bitmap bmp = new Bitmap(Properties.Resources.开始测试);
            e.Graphics.DrawImage(bmp, 0, 0, startPanel.Width, startPanel.Height);
        }

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {
            DrawCaption(serialPanel);
            DrawCaption(stressSensor);
            DrawCaption(bussinessPanel);
            DrawCaption(recordPanel);
            DrawCaption(startPanel);
            DrawCaption(channelPanel);
        }

        private void channelPanel_Paint(object sender, PaintEventArgs e)
        {
            Bitmap bmp = new Bitmap(Properties.Resources.通道配置);
            e.Graphics.DrawImage(bmp, 0, 0, startPanel.Width, startPanel.Height);
        }
        private void DrawCaption(Panel panel)
        {
            System.Drawing.Font font = new Font("微软雅黑", 9, FontStyle.Regular);
            SolidBrush brush = new SolidBrush(Color.Black);
            Graphics g = tableLayoutPanel1.CreateGraphics();
            g.SmoothingMode = SmoothingMode.HighSpeed;
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
            int originX = panel.Location.X + 8;
            int originY = panel.Location.Y + panel.Size.Height + 3;
            
            if(panel == serialPanel)
            {
                g.DrawString("串口配置", font, brush, originX, originY);
            }
            else if(panel == stressSensor)
            {
                g.DrawString("型号选择", font, brush, originX, originY);
            }
            else if(panel == bussinessPanel)
            {
                g.DrawString("业务管理", font, brush, originX, originY);
            }
            else if(panel == recordPanel)
            {
                g.DrawString("记录查询", font, brush, originX, originY);
            }
            else if(panel == channelPanel)
            {
                g.DrawString("通道配置", font, brush, originX, originY);
            }
            else if(panel == startPanel)
            {
                g.DrawString("开始测试", font, brush, originX, originY);
            }
        }

        private void serialPanel_Click(object sender, EventArgs e)
        {
            SerilPortForm form = new SerilPortForm();
            form.ShowDialog();
        }

        private void bussinessPanel_Click(object sender, EventArgs e)
        {
            BussinessForm form = new BussinessForm();
            form.FormClosing += (s,m) => this.Show();//关闭子窗体时显示主窗体
            form.Show();
            this.Hide();
        }

        private void recordPanel_Click(object sender, EventArgs e)
        {
            UI.QueryForm form = new QueryForm();
            form.FormClosing += (s, m) => this.Show();//关闭子窗体时显示主窗体
            form.Show();
            this.Hide();
        }

        private void stressSensor_Click(object sender, EventArgs e)
        {
            ProductionChoice form = new ProductionChoice();
            form.TransInfoEvent += s => { CurrentProduction = s; _C34970AConfiger.SetDefaultChannel(s.ResistanceCount); };
            form.ShowDialog();
        }

        private void startPanel_Click(object sender, EventArgs e)
        {
            Form1 form = new Form1(CurrentProduction,ThisEmployee);
            form.FormClosed += (s, m) => this.Show();//关闭子窗体时显示主窗体
            if(form.CanOpenPort == true)
            {
                form.Show();
                this.Hide();
            }
        }

        private void channelPanel_Click(object sender, EventArgs e)
        {
            ChannelChoice form = new ChannelChoice(CurrentProduction);
            form.ShowDialog();
        }
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct MARGINS
    {
        public int Left;
        public int Right;
        public int Top;
        public int Bottom;
    }
}
